unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Panel1: TPanel;
    Button1: TButton;
    ScrollBar1: TScrollBar;
    Timer2: TTimer;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Draw;
    procedure Timer1Timer(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  h=50;
  N=20;

var
  Form1: TForm1;
  massP: array of TPanel;
  mass: array of integer;
  t, i, j, k, a,s, f: integer;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  s:=1;
  k:=1;
  Timer1.Interval:=50;
  Timer2.Interval:=50;
  setlength (mass, N);
  setlength (massP, N);
  for i:= 0 to N-1 do
    mass[i]:=random(100);
  for i:= 0 to N-1 do
  begin
    massP[i]:=TPanel.Create(Form1);
    massP[i].Top:=0;
    massP[i].Left:=i*h;
    massP[i].Height:=h;
    massP[i].Width:=h;
    massP[i].Parent:=Form1;
    massP[i].Caption:=IntToStr(mass[i]);
  end;
  Draw;
end;

procedure TForm1.Draw;
begin
  for i:= 0 to N-1 do
  begin
    massP[i].Caption:=IntToStr(mass[i]);
    massP[i].Top:=0;
    massP[i].Left:=i*h;
    if (i<>k)  then
      massP[i].Color:=clWhite;
    if (i=k) then
      massP[i].Color:=clBlue;
    if (i=t) or (i=t+1) then
      massP[i].Color:=clGreen;
  end;
end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (k=N) then
  begin
    Timer1.Enabled:=false;
    for i:=0 to N-1 do
      massP[i].Color:=clWhite;
    Application.MessageBox('Sorting is Compelete.', 'The end');
    exit;
  end;
  Draw;
  if (s<>0) then
  begin
  for i:= 0 to k-2 do
  begin
    if (mass[k]>=mass[i]) and (mass[k]<=mass[i+1]) then
    begin
      t:=i;
      s:=0;
      Draw;
      Timer1.Enabled:=false;
      Timer2.Enabled:=true;
      exit;
    end;
    if (mass[k]<=mass[0]) then
     begin
      t:=-1;
      s:=0;
      Draw;
      Timer1.Enabled:=false;
      Timer2.Enabled:=true;
      exit;
    end;
  end;
  end;
  k:=k+1;
  s:=1;
end;



procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
  Timer2.Interval:=ScrollBar1.Position;
  Timer1.Interval:=ScrollBar1.Position;
  Label1.Caption:='Delay time: ' + IntToStr(ScrollBar1.Position) + ' ms';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Enabled:=false;
  Timer2.Enabled:=false;
  s:=1;
  k:=1;
  setlength (mass, N);
  setlength (massP, N);
  for i:= 0 to N-1 do
    mass[i]:=random(100);
  for i:= 0 to N-1 do
  begin
    massP[i].Top:=0;
    massP[i].Left:=i*h;
    massP[i].Height:=h;
    massP[i].Width:=h;
    massP[i].Caption:=IntToStr(mass[i]);
  end;
  Draw;
  Timer1.Enabled:=true;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  if(massP[k].Top<>h) and (massP[t+1].Left=(t+1)*h) then
    massP[k].Top:=massP[k].Top+5;
  if (massP[k].Top = h) and (massP[t+1].Left <> (t+2)*h) then
    for i:= t+1 to k-1 do
      massP[i].Left:=massP[i].Left+5;
  if (massP[k].left<>h*(t+1)) and (massP[t+1].Left = (t+2)*h) then
    massP[k].Left:=massP[k].Left-5;
  if (massP[k].Left = h*(t+1)) and (massP[k].Top<>0) then
    massP[k].Top:=massP[k].Top-5;
  if (massP[k].Top=0) and (massP[k].Left=(t+1)*h) and (massP[t+1].Left =(t+2)*h) then
  begin
    a:=mass[k];
    for i:=k-1 downto t+1 do
      mass[i+1]:=mass[i];
    mass[t+1]:=a;
    Draw;
    s:=1;
    k:=k+1;
    t:=-2;
    Timer1.Enabled:=true;
    Timer2.Enabled:=false;
  end;

end;




end.
